<?php
declare(strict_types=1);
namespace Service\VenderClient\ShareCenter;
use Service\Code\Normal;
use Service\BaseService;
use Service\ExtraService\TaskWorker;

class ShareCenterApi extends BaseService
{
    protected $config;
    protected $router = [
        'detect'             => 'https://api-cn.faceplusplus.com/facepp/v3/detect',//人脸检测 API
        'faceset_create'     => 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create',//创建人脸库 API
        'faceset_delete'     => 'https://api-cn.faceplusplus.com/facepp/v3/faceset/delete',//删除人脸库 API
        'faceset_update'     => 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update',//更新人脸库 API
        'faceset_getfacesets'=> 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets',//获取人脸库列表及信息 API
        'faceset_getdetail'  => 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail',//获取人脸库信息 API
        'faceset_addface'    => 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface',//添加人脸 API
        'faceset_removeface' => 'https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface',//获取openid
        'search'             => 'https://api-cn.faceplusplus.com/facepp/v3/search',//人脸搜索 API，
        'ocridcard'          => '/Face/ocridcard',//身份证识别
        'messageDeal'        => '/Face/messagedeal'//消息处理
    ];
    public function __construct( array $configBody = [] )
    {
        if(!empty($configBody)){
            $this->config = $configBody;
        }else{
            $gateway=env('SERVICE_SHARE_CENTER','');
            if(!$gateway){
                throw new \Exception('处理失败【图形处理中心地址未配置，请联系管理员配置】 ' );
            }
            $account=env('SERVICE_SHARE_CENTER_ACCOUNT','');
            if(!$account){
                throw new \Exception('处理失败【图形处理中心用户未配置，请联系管理员配置】 ' );
            }
            $this->config=[
                'gateway'=> $gateway,
                'account'=>$account
            ];
        }
    }
    /**
     * 身份证识别
     * @param type $param
     * @return type
     */
    public function ocrIdCard($param) {
        if(empty($param['image_base64'])){
            return Normal::returnCode(Normal::FAIL,'图片的base64加密的二进制数据必传');
        }
        $data=[
            'image_base64'=>$param['image_base64'],
        ];
        $response=$this->execute('ocridcard', $data);
        return $response;
    }
    
    /**
     * 消息处理
     * @param type $param
     * @return type
     */
    public function messageDeal(array $param) {        
        if(empty($param)){
            return Normal::returnCode(Normal::FAIL,'参数必传');
        }
        $response=$this->execute('messageDeal', $param);
        return $response;
    }
    
    public function execute(string $method, array $data){
        $messageData= array_merge($data,['account'=> $this->config['account']]);
        $parameter = [
            [
                'tag'       => 'faceplus_'.$method,
                'url'       => $this->config['gateway'].$this->router[$method],
                'data'      => $messageData,
                'method'    => 'post',
                'format'    => 'x-www-form-urlencoded',
                'header'    => [
                ],
            ],
        ];       
        $resTask    = TaskWorker::instance()->addTask($parameter);
        $resTask    = current($resTask);        
        if($resTask['response']['httpCode']!='200'){
            return Normal::returnCode(Normal::FAIL,'图像识别服务器中心请求失败:'.$resTask['response']['error']);
        }
        $response   = @json_decode($resTask['response']['result'],true);
        return $response;
    }
}
